import Nuxt from 'nuxt';
import express from 'express';
import proxy from 'express-http-proxy';
// import api from './api'
const app = express();
const host = process.env.HOST || '127.0.0.1';
const port = process.env.PORT || 3000;
app.set('port', port);
// 代理设置转发
app.use('/api/*', proxy('localhost:8080', {
    // https: true,
    proxyReqPathResolver: function (req) {
        return req.baseUrl.replace(new RegExp(/(\/api)/g), '');
    },
    // forwardPath: function (req, res) {
    // req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    // req.headers['accept'] = '*/*';
    // return req._parsedUrl.path;
    // return req.baseUrl.replace(new RegExp(/(\/api)/g), '');
    // }
}));

// Import API Routes
// app.use('/api', api)

// Import and Set Nuxt.js options
let config = require('../nuxt.config.js');
config.dev = !(process.env.NODE_ENV === 'production');

// Init Nuxt.js
const nuxt = new Nuxt(config);
app.use(nuxt.render);

// Build only in dev mode
if (config.dev) {
    nuxt.build().catch((error) => {
        console.error(error); // eslint-disable-line no-console
        process.exit(1)
    });
}
// Listen the server
app.listen(port, host);
console.log('Server listening on ' + host + ':' + port); // eslint-disable-line no-console
